#include "Login.h"
#include <json/json.h>
#include <filesystem>
#include <memory>
#include <ostream>
#include <sstream>
#include <tuple>
#include <vector>
#include "HtmlUtil.h"
#include "FileUtil.h"
#include "PQUtil.h"
#include "PdfUtil.h"
std::vector<std::vector<std::string>> assetsCached;
std::vector<std::tuple<std::string,std::string>> get_files(std::string& dir,std::string& web_dir){
   // ../assets/Index/dist/assets
    auto ret = std::vector<std::tuple<std::string,std::string>>();
    std::filesystem::path p(dir);
    if(std::filesystem::is_directory(p)){
        for(std::filesystem::path elem : std::filesystem::directory_iterator(p)){
            if(elem.extension()==".js" ){
                ret.push_back(
                    std::make_tuple(
                        web_dir+'/'+elem.filename().string(),
                        "js"
                    ));
            }
            else if(elem.extension() == ".css"){
                ret.push_back(
                    std::make_tuple(
                        web_dir+'/'+elem.filename().string(),
                        "css"
                    ));
            }
         }
    }
    else{
        std::cout<<"not directory"<<std::endl;
    }
    return ret;
}

std::vector<std::vector<std::string>> getAssetsUrl()
{


   std::string path {"/data/data/com.termux/files/home/storage/downloads/books/cpp2/stl/C++ Templates The Complete Guide.pdf"};
   // PQUtil::updateCppPdf(path);
   // PdfUtil::testDraw();

  if(!assetsCached.empty()){
    LOG_DEBUG << "(HomeController) - get assets from cache";
    // return assetsCached; // TODO delete comment in product enviroment
  }
  std::vector<std::string> jsUrls;
  std::vector<std::string> cssUrls;

    std::string dir("../assets/Index/dist/assets");
    std::string web_dir("Index/dist/assets");
    auto ff =  get_files(dir,web_dir);
    for(auto f :ff){
       std::string name,extension;
       std::tie(name,extension) = f;
       if(extension =="js"){
         jsUrls.push_back(name);
       }
       else{
         cssUrls.push_back(name);
       }
    }
  assetsCached = {jsUrls, cssUrls};
  return assetsCached;
}

void Login::getIndex(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback)
{
  LOG_DEBUG<<"(HomeController) - GET INDEX";

  auto assetsUrl = getAssetsUrl();

  std::vector<std::string> js = assetsUrl[0];
  std::vector<std::string> css = assetsUrl[1];

  HttpViewData data;
  data.insert("baseUrl", "/login");
  data.insert("css", css);
  data.insert("js", js);

  auto resp = HttpResponse::newHttpViewResponse("Index.csp", data);

  callback(resp);

}


/*
 std::string str ;
 auto files = FileUtil::mdcodes(str);
 for(auto f:files){
    auto [desc,code] = f;
     std::cout<<"XXXX:"<<desc<<std::endl;
     std::cout<<code<<std::endl;
 }
 */

/*

 std::string file = "/data/data/com.termux/files/home/wiki/cpp/cpp-docs";
 std::string ext = ".md";
 auto files = FileUtil::readDirRecurs(file, ext);
 for(auto f:files){
     std::cout<<f<<std::endl;
   auto ret = FileUtil::mdcodes(f);
   PQUtil::updateCppDoc(f,ret);
 }
*/
// PQUtil::test();
 /*
 std::string file = "/data/data/com.termux/files/home/wiki/psql";
 std::string ext = ".html";
 auto files = FileUtil::readDir(file, ext);
 for(auto f:files){
   auto ret = HtmlUtil::getPsqlCode(f);
   PQUtil::updatePsql(f,ret);
 }
 */

 /*
 std::string file = "/data/data/com.termux/files/usr/share/nvim/runtime/doc";
 std::string ext = ".txt";
 auto files = FileUtil::readDir(file, ext);
 for(auto f:files){
   auto codes = FileUtil::vimdoc(f);
   PQUtil::updateVim(f,codes);
 }
*/

/*
 std::string file = "/data/data/com.termux/files/home/wiki/cpp/html-book-20220730/reference/en/cpp";
 std::string ext = ".html";
 auto files = FileUtil::readDirRecurs(file, ext);
 for(auto f:files){
     std::cout<<f<<std::endl;
     auto ret = HtmlUtil::getCppSource(f);
     PQUtil::updateCpp(f,ret);
 }
 */
 /*
 std::string file = "/data/data/com.termux/files/usr/share/doc/rust/html";
 std::string ext = ".html";
 auto files = FileUtil::readDirRecurs(file, ext);
 for(auto f:files){
     std::cout<<f<<std::endl;
     auto ret = HtmlUtil::getRustSource(f);
     PQUtil::updateRust(f,ret);
 }
 std::cout<<"Read Completed"<<std::endl;
*/

/*

  std::ifstream json_file("../assets.json", std::ifstream::binary);
  Json::Value assets;
  json_file >> assets;
  for(auto asset: assets){
    std::string urlJs = asset["js"].asString();
    std::string urlCss = asset["css"].asString();
    if(!urlCss.empty()) {
      cssUrls.push_back(urlCss);
    }
    if(!urlJs.empty()) {
      jsUrls.push_back(urlJs);
    }
  }
  */
